package com.cha.chaApp.babyphysical.controller;

import com.cha.chaApp.babyphysical.param.BabyPhysicalIndicatorsParam;
import com.cha.chaApp.babyphysical.param.UpdateBabyPhysicalIndicatorsParam;
import com.cha.chaApp.babyphysical.service.BabyPhysicalIndicatorsService;
import com.cha.chaApp.babyphysical.vo.BabyAgeInfoVo;
import com.cha.chaApp.babyphysical.vo.BabyGrowthInfoVo;
import com.cha.chaApp.babyphysical.vo.ComparisonResultVo;
import com.cha.chaCommon.core.common.R;
import com.cha.chaCommon.core.entity.BabyPhysicalIndicators;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "宝宝身体指标管理")
@RestController
@RequestMapping("/baby/physical/indicators")
public class BabyPhysicalIndicatorsController {

    @Autowired
    private BabyPhysicalIndicatorsService babyPhysicalIndicatorsService;

    // 通过baby_id查询，没有数据时返回空结果
    @ApiOperation(
            value ="通过baby_id查询宝宝身体指标信息",
            notes = "根据提供的宝宝ID查询宝宝的身体指标信息列表。如果未找到数据，将返回一个空列表。")
    @GetMapping("/getBabyPhysicalIndicators")
    public R getBabyPhysicalIndicators(
            @ApiParam(value = "宝宝id", required = true, example = "1")
            @RequestParam(required = false) Long babyId) {
        List<BabyPhysicalIndicators> list = babyPhysicalIndicatorsService.getBabyPhysicalIndicatorsByBabyId(babyId);
        return R.ok(list);
    }

    // 添加宝宝身体指标信息的接口
    @ApiOperation(
            value = "添加宝宝身体指标信息",
            notes = "添加 baby_physical_indicators_table 表中的身体指标信息。")
    @PostMapping("/add")
    public R addBabyPhysicalIndicators(
             @ApiParam( value = "身体指标参数对象", required = true)
            @RequestBody BabyPhysicalIndicatorsParam indicatorsParam) {
        try {
            boolean result = babyPhysicalIndicatorsService.addBabyPhysicalIndicators(indicatorsParam);
            if (result) {
                return R.ok("宝宝身体指标信息添加成功");
            } else {
                return R.fail("宝宝身体指标信息添加失败");
            }
        } catch (Exception e) {
            return R.fail("添加宝宝身体指标信息时发生异常: " + e.getMessage());
        }
    }

    // 更新部分字段的接口，根据indicator_id更新指定字段
    @ApiOperation(
            value = "根据indicator_id更新指定字段",
            notes = "根据提供的indicator_id更新 baby_physical_indicators_table 表中指定的字段。")
    @PutMapping("/updateSomeFields")
    public R updateSomeFields(
            @ApiParam(value = "身体指标参数对象", required = true, example = "1")
            @RequestBody UpdateBabyPhysicalIndicatorsParam indicators) {
        try {
            boolean result = babyPhysicalIndicatorsService.updateSomeFieldsById(indicators.getIndicatorId(), indicators);
            if (result) {
                return R.ok("宝宝身体指标信息更新成功");
            } else {
                return R.fail("宝宝身体指标信息更新失败");
            }
        } catch (Exception e) {
            return R.fail("更新宝宝身体指标信息时发生异常: " + e.getMessage());
        }
    }

    // 删除宝宝身体指标信息（根据指标ID）的接口
    @ApiOperation(
            value = "根据indicator_id删除宝宝身体指标信息",
            notes = "根据提供的indicator_id删除 baby_physical_indicators_table 表中的宝宝身体指标信息。")
    @DeleteMapping("/{indicatorId}")
    public R deleteBabyPhysicalIndicatorsById(
            @ApiParam(value = "身体指标id", required = true, example = "1")
            @PathVariable Long indicatorId) {
        try {
            boolean result = babyPhysicalIndicatorsService.deleteBabyPhysicalIndicatorsById(indicatorId);
            if (result) {
                return R.ok("宝宝身体指标信息删除成功");
            } else {
                return R.fail("宝宝身体指标信息删除失败");
            }
        } catch (Exception e) {
            return R.fail("删除宝宝身体指标信息时发生异常: " + e.getMessage());
        }
    }

    // 查询月龄身高体重头围的接口
    @ApiOperation(
            value = "根据baby_id查询月龄身高体重头围信息",
            notes = "根据提供的baby_id查询 baby_physical_indicators_table 表和baby_info_table表中的信息:身高,体重,头围,并计算月龄。")
    @GetMapping("/getBabyGrowthInfo")
    public R getBabyGrowthInfo(
            @ApiParam(value = "宝宝id", required = true, example = "1")
            @RequestParam Long babyId) {
        List<BabyGrowthInfoVo> list = babyPhysicalIndicatorsService.getBabyGrowthInfoByBabyId(babyId);
        return R.ok(list);
    }

    // 查询宝宝年龄信息（根据measurement_time和birth_date对比）
    @ApiOperation(
            value = "根据baby_id查询宝宝年龄信息",
            notes = "根据提供的baby_id查询 baby_info_table 表和 baby_physical_indicators_table 表中的信息，并计算宝宝的年龄。")
    @GetMapping("/getBabyAgeInfo")
    public R getBabyAgeInfo(
            @ApiParam(value = "宝宝id", required = true, example = "1")
            @RequestParam Long babyId) {
        List<BabyAgeInfoVo> list = babyPhysicalIndicatorsService.getBabyAgeInfoByBabyId(babyId);
        return R.ok(list);
    }

    // 根据indicator_id获取对比结果及建议的接口
    @ApiOperation(
            value = "根据indicator_id获取对比结果及建议",
            notes = "根据提供的indicator_id查询 baby_physical_indicators_table 表和 baby_standard_data_table 中的信息，并计算对比结果和建议。")
    @GetMapping("/getComparisonResult/{indicatorId}")
    public R getComparisonResult(
            @ApiParam(value = "身体指标id", required = true, example = "1")
            @PathVariable Long indicatorId) {
        try {
            ComparisonResultVo resultVo = babyPhysicalIndicatorsService.getComparisonResultByIndicatorId(indicatorId);
            return R.ok(resultVo);
        } catch (Exception e) {
            return R.fail("获取对比结果时发生异常: " + e.getMessage());
        }
    }
}